/**
 * 用户表的数据模型
 * @type {User}
 */
/*******************/
module.exports = class User extends require('./model') {

  /**
   *
   * 通过用户名和密码获取用户
   * @param {*} username 用户名
   * @param {*} password 用户密码
   * @returns {Promise<通过用户名和密码获取用户>}
   */
  static getUserByUnameAndPwd (username , password) {
    return new Promise((resolve,reject) => {
      let sql ='SELECT * FROM `user` WHERE username = ? AND password = ?';

      console.log('通过用户名和密码获取用户sql='+sql);
      console.log('username='+username+';password='+password+';-----------------------');

      this.dbQuery(sql , [username , password])
        .then(results => {
        resolve(results[0]);//因为只获取1条数据，所以要加上此
      })
        .catch(error => {
        console.log(`通过用户名和密码获取用户失败：${ error.message }`)
        reject(error);
      })
    })
  }

  /***
   * 通过用户名获取用户信息方法
   * @param username
   * @returns {Promise<通过用户名获取用户信息方法>}
   */
  static getUserByUsername (username) {
    return new Promise((resolve, reject) => {
      let sql = 'SELECT * FROM `user` WHERE username = ?';

      console.log('通过用户名获取用户sql');
      console.log('username=' + username+';--------------------------');

      this.dbQuery(sql, [username])
        .then(result => {
          resolve(result[0])
        })
        .catch(error => {
          console.log(`通过用户名获取用户失败:${error.message}`);
          reject(error);
        })
    })
  }

  // static getUserByUsernameOrNameDim (username, name) {
  //   return new Promise((resolve, reject) => {
  //     let sql = `SELECT * From user WHERE 'username' LIKE %${username}% OR 'name' LIKE %${name}%`
  //   })
  // }

  /**
   * 获取指定用户的记录条数
   * @param sex
   * @param business
   * @param username
   * @param name
   * @returns {Promise<获取指定用户的记录条数>}
   */
  static getUserCount(sex, business, username_or_name) {
    return new Promise((resolve, reject) => {
      // 否则拼接时出现直接粘连,所以在每次拼接结束和开始时加上空格
      let sql = 'SELECT COUNT(1) AS count FROM `user` WHERE 1 = 1 ';
      sql += sex != 0 && sex ? `AND sex = ${sex} ` : '';
      sql += business != 0 && business ? ` AND business = ${business} ` : '';
      sql += username_or_name != '' && username_or_name ? ` AND (username LIKE '%${username_or_name}%' ` : '';
      sql += username_or_name != '' && username_or_name ? ` OR name LIKE '%${username_or_name}%') ` : '';

      console.log('获取用户数量sql=');
      console.log('sex=' + sex + ';business=' + business + ';username_or_name=' + username_or_name + ';------------------------');

      this.dbQuery(sql)
        .then(result => {
          resolve(result[0]) // 只要1条account记录,所以加上[0]
        })
        .catch(error => {
          console.log('获取用户数量失败');
          reject(error)
        })
    })
  }

  /**
   * 获取指定条数记录的用户
   * @param start
   * @param size
   * @param sex
   * @param business
   * @param username_or_name
   * @returns {Promise<获取指定条数记录的用户>}
   */
  static getUserRowCountRecord(start, size, sex, business, username_or_name) {
    return new Promise((resolve, reject) => {
      // 否则拼接时出现直接粘连,所以在每次拼接结束和开始时加上空格,注意:拼接的判断是不能用全等于,即'===';
      let sql = 'SELECT *FROM `user` WHERE 1 = 1 ';
      sql += sex != 0 && sex ? `AND sex = ${sex} ` : ' ';
      sql += business != 0 && business ? ` AND business = ${business} ` : ' ';
      sql += username_or_name != '' && username_or_name ? ` AND (username LIKE '%${username_or_name}%' ` : ' ';
      sql += username_or_name != '' && username_or_name ? ` OR name LIKE '%${username_or_name}%') ` : ' ';

      sql += 'LIMIT ?, ?'; // 最终都会把这条语句拼接上

      console.log('获取指定条数记录的用户sql=' + sql);
      console.log('start=' + start + ';size=' + size + ';sex=' + sex + ';business=' + business +
        ';username_or_name=' + username_or_name + ';------------------------');

      this.dbQuery(sql, [start, size])
        .then(result => {
          resolve(result)
        })
        .catch(error => {
          console.log('获取指定条数记录的用户失败');
          reject(error)
        })

    })
  }


  /**
   * 写入用户至数据库
   * @param user
   * @returns {Promise<写入用户至数据库>}
   */
  static setUser (user) {
    return new Promise((resolve, reject) => {
      let sql = 'INSERT INTO user SET ?';

      console.log('插入用户sql='+sql);
      console.log('username='+user.username+';password='+user.password+'--------------------------');

      this.dbQuery(sql , user )
        .then(results => {
        resolve(results.insertId);  //插入成功，则返回插入成功数据的id
      })
        .catch(error => {
        console.log(`插入用户失败，${error.message}`)
        reject(error);
      })
    })
  }

  /**
   * 更新用户
   * @param user
   * @returns {Promise<更新用户>}
   */
  static updateUser(user) {
    return new Promise((resolve, reject) => {
      let sql = 'UPDATE `user` SET `password` = ?, `name` = ?, sex = ?, birthday = ?, tel = ?, address = ?, money = ?, business = ? WHERE username = ?'

      console.log('更新用户sql='+sql);
      console.log('username=' + user.username + ';password=' + user.password + ';name=' + user.name+';sex=' + user.sex + ';birthday=' + user.birthday
      + ';tel=' + user.tel + ';address=' + user.address + ';money=' + user.money +
        ';business=' + user.business + '--------------------------');

      this.dbQuery(sql , [user.password, user.name, user.sex, user.birthday, user.tel, user.address, user.money, user.business, user.username] )
        .then(results => {
        resolve(results.affectedRows);  //插入成功，则返回插入成功数据的id
      })
        .catch(error => {
        console.log(`更新用户失败，${error.message}`)
        reject(error);
      })
    })
  }

  /**
   * 删除用户方法
   * @param username 删除的用户名
   * @returns {Promise<删除用户方法>}
   */
  static deleteUser(username) {
    return new Promise((resolve, reject) => {
      let sql = 'DELETE FROM `user` WHERE username = ?';

      console.log('删除用户sql=' + sql);
      console.log('username=' + username + ';---------------------------------------------');

      this.dbQuery(sql, username)
        .then(result => {
          resolve(result.affectedRows); // 删除成功返回受影响行数
        })
        .catch(error => {
          console.log('删除用户' + username + '失败' + error);
          reject(error);
        })
    })
  }


}
